// pages/videorank/videorank.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    currentTab: 0,
    showLoading: true,
    videoranklistflag: true,
    videoranklist: [],
    res_navbar: ['排名', '大赛详情']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.refreshvideoranklist()
    var pages = getCurrentPages();
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        videosferesh: true,
        promotefresh: false
      })
    }
  },
  onResNavbarTap: function(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    });
  },
  refreshvideoranklist: function() {
    var that = this
    shihan.sendRequest(config.videorank + that.data.page + '-' + that.data.page_size + '.html', 'GET', {}, {})
      .then(function(response) {
        if (response.data.code == 0 && response.data.data.sh_items.length > 0) {
          wx.stopPullDownRefresh()
          that.setData({
            page: that.data.page + 1,
            videoranklist: that.data.videoranklist.concat(response.data.data.sh_items),
            showLoading: false,
            prohasMore: true,
            videoranklistflag: true
          })
        } else {
          wx.stopPullDownRefresh()
          that.setData({
            showLoading: false,
            prohasMore: false,
            videoranklistflag: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  onSearchInput: function(ev) { // 搜索输入值时的操作
    this.setData({
      searchKeyword: ev.detail.value
    });
  },
  // 搜索确认搜索
  onSearchConfirm: function(ev) {
    this.setData({
      page: 1,
      videoranklist: [],
      showLoading: true,
      videoranklistflag: true
    })
    if (this.data.videoranklist.length == 0) {
      if (this.data.searchKeyword) {
        this.searchvideoranklist()
      } else {
        this.refreshvideoranklist()
      }
    }
  },
  searchvideoranklist: function() {
    var that = this
    shihan.sendRequest(config.searchrank + that.data.searchKeyword + '/' + that.data.page + '-' + that.data.page_size + '.html', 'GET', {}, {})
      .then(function(response) {
        if (response.data.code == 0 && response.data.data.sh_items.length > 0) {
          wx.stopPullDownRefresh()
          that.setData({
            page: that.data.page + 1,
            videoranklist: that.data.videoranklist.concat(response.data.data.sh_items),
            showLoading: false,
            prohasMore: true,
            videoranklistflag: true
          })
        } else {
          that.setData({
            showLoading: false,
            prohasMore: false,
            videoranklistflag: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      videoranklist: [],
      showLoading: true,
      searchKeyword: '',
      videoranklistflag: true
    })
    if (this.data.videoranklist.length == 0) {
      this.refreshvideoranklist()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.videoranklistflag) {
      this.setData({
        videoranklistflag: false
      })
      if (this.data.searchKeyword) {
        this.searchvideoranklist()
      } else {
        this.refreshvideoranklist()
      }
    }
  }
})